package com.example.administrator.live;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by Administrator on 2017/6/28.
 */

public class MyWebView extends WebView {

    private JsCallJava js;

    public MyWebView(Context context) {
        super(context);
        init();
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        js = new JsCallJava();

        setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("----------", "shouldOverrideUrlLoading: " + url );
                String[] strings = url.split(":");
                for (int i = 0;strings!=null&& i < strings.length; i++) {

                    if(i==3){
                        try {
                            strings[i] = URLDecoder.decode(strings[i],"UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    Log.e("-----", "shouldOverrideUrlLoading: "+strings[i] );
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                js.onPageStarted(view,url);
            }
        });
    }

    @Override
    public void addJavascriptInterface(Object object, String name) {
        js.addJavascriptInterfaces(object,name);
    }
}
